package com.zhiguang.cms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.zhiguang.cms.entity.Article;
import com.zhiguang.cms.entity.Category;
import com.zhiguang.cms.entity.Channel;
import com.zhiguang.cms.entity.Link;
import com.zhiguang.cms.service.ArticleService;
import com.zhiguang.cms.service.LinkService;

@Controller
public class IndexController {
	
	@Autowired
	ArticleService articleService;
	
	@Autowired
	LinkService linkService;
	
	
	/**
	 * 显示一个栏目下的文章  与首页极其类似
	 * @param request
	 * @param page
	 * @param channelId
	 * @param categoryId
	 * @return
	 */
	@RequestMapping("channel")
	public String index(HttpServletRequest request,
			@RequestParam(defaultValue="1") int page,
			@RequestParam(defaultValue="1") int channelId, 
			@RequestParam(defaultValue="0") int categoryId) {
		
		// 获取所有的栏目
		  Thread t1 = 	new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					List<Channel> listChannels = articleService.listChannels();
					request.setAttribute("channels", listChannels);
				}
			};
			
			// 获取当前栏目下的所有的分类
			  Thread t11 = 	new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						List<Category> categories = articleService.listCat(channelId)	;
						request.setAttribute("categories", categories);
					}
				};
			
			// 获取最新文章
			  Thread t2 = 	new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						List<Article> lastArticles = articleService.lastarticles(10);
						request.setAttribute("lastArticles", lastArticles);
					}
				};
				
			
			// 获取当前栏目或者当前的分类下的文章
			  Thread t3 = 	new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						PageInfo<Article> articlePage = articleService.listBy2C(channelId,categoryId,
								page);
						request.setAttribute("articlePage", articlePage);
					}
				};
				
				// 获取友情链接
				  Thread t4 = 	new Thread() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							PageInfo<Link> pageLink = linkService.list(1);
							request.setAttribute("pageLink", pageLink);
						}
					};
				
				
			t1.start();
			t11.start();
			t2.start();
			t3.start();
			t4.start();
			
			/*等待子线程结束之后才继续执行主线程*/
			try {
				t1.join();
				t11.join();
				t2.join();
				t3.join();
				t4.join();
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			request.setAttribute("channelId", channelId);
			request.setAttribute("categoryId", categoryId);
			
			return "channels";
		
	}
	
	@RequestMapping({"/","index"})
	public String index(HttpServletRequest request,
			@RequestParam(defaultValue="1") int page) {
		
		// 获取所有的栏目
	  Thread t1 = 	new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<Channel> listChannels = articleService.listChannels();
				request.setAttribute("channels", listChannels);
			}
		};
		
		// 获取最新文章
		  Thread t2 = 	new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					List<Article> lastArticles = articleService.lastarticles(10);
					request.setAttribute("lastArticles", lastArticles);
				}
			};
			
		
		// 获取热门文章
		  Thread t3 = 	new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					PageInfo<Article> articlePage = articleService.hotList(page);
					request.setAttribute("articlePage", articlePage);
				}
			};
			
			// 获取友情链接
			  Thread t4 = 	new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						PageInfo<Link> pageLink = linkService.list(1);
						request.setAttribute("pageLink", pageLink);
					}
				};
			
			
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
		/*等待子线程结束之后才继续执行主线程*/
		try {
			t1.join();
			t2.join();
			t3.join();
			t4.join();
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "index";
	}

}
